package com.gxa.customer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxa.common.commons.R;
import com.gxa.common.params.AddPartnerParam;
import com.gxa.common.params.QueryPartnerParam;
import com.gxa.common.params.UpdatePrParam;
import com.gxa.common.pojo.ClientInfo;
import com.gxa.common.pojo.PartnerCustomer;
import com.gxa.common.pojo.PartnerInfo;
import com.gxa.common.utils.RUtils;
import com.gxa.customer.mapper.PartnerCustomerMapper;
import com.gxa.customer.mapper.PartnerMapper;
import com.gxa.customer.service.PartnerService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Service
public class PartnerServiceIml implements PartnerService {
    @Autowired
    private PartnerMapper partnerMapper;
    @Autowired
    private PartnerCustomerMapper partnerCustomerMapper;
    @Override
    public R addPartner(AddPartnerParam param) {
        PartnerInfo partnerInfo = new PartnerInfo();
        BeanUtils.copyProperties(param,partnerInfo);
        partnerInfo.setCustomerCount(0);
        partnerMapper.insert(partnerInfo);
        return RUtils.success();
    }

    @Override
    public R queryPartner(QueryPartnerParam param) {
        QueryWrapper<PartnerInfo> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(param.getId())){
            wrapper.eq("id", param.getId());
            //todo
            //将查询客户信息列表
        };
        if (Objects.nonNull(param.getNationId())){
            wrapper.eq("nation_id", param.getNationId());
        };
        if(Strings.isNotBlank(param.getName())){
            wrapper.like("name",param.getName());
        };
        if(Strings.isNotBlank(param.getNation())){
            wrapper.eq("nation",param.getNation());
        };
        if(Strings.isNotBlank(param.getRegionSales())){
            wrapper.eq("region_sales",param.getRegionSales());
        };
        if(Strings.isNotBlank(param.getInternalEditor())){
            wrapper.eq("internal_editor",param.getInternalEditor());
        };
        if(Strings.isNotBlank(param.getExternalEditor())){
            wrapper.eq("external_editor",param.getExternalEditor());
        };
        List<PartnerInfo> list = partnerMapper.selectList(wrapper);
        return RUtils.success(list);
    }

    @Override
    public R updatePartner(UpdatePrParam param) {
        PartnerInfo partnerInfo = new PartnerInfo();
        BeanUtils.copyProperties(param,partnerInfo);
        partnerMapper.updateById(partnerInfo);
        return RUtils.success();
    }

    @Override
    public R findCustomerListByPartnerInfo(Long id,Integer currentPage, Integer pageSize) {
       //查询出合作方信息和属于合作方id的所有客户
        PartnerInfo partnerInfo = partnerMapper.selectById(id);
        Integer startPage = (currentPage-1)*pageSize;
        List<ClientInfo> clientInfoList = partnerMapper.findCustomerListByPartnerInfo(id,startPage,pageSize);
        partnerInfo.setClientInfoList(clientInfoList);
        return RUtils.success(partnerInfo);
    }
}
